<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="queryParams" ref="queryParamsRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="业务code" prop="businessCode">
          <el-input
              v-model="queryParams.businessCode"
              placeholder="请输入业务code"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作人员" prop="operName">
          <el-input
              v-model="queryParams.operName"
              placeholder="请输入操作人员"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择操作状态" class="width-120" clearable>
            <el-option
                v-for="item in statusDict"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间" prop="operTime">
          <el-date-picker clearable
                          v-model="queryParams.operTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            @click="handleExport"
            v-hasPerm="['msip:log:export']"
        >
          <template #icon>
            <i-ep-download/>
          </template>
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :data="logList" :border="true" :stripe="true"
              highlight-current-row @selection-change="handleSelectionChange" :header-cell-style="$headerCellStyle">
      <el-table-column type="selection" width="55" :align="'center'"/>
      <el-table-column label="日志主键" :align="'center'" prop="id" v-if="false"/>
      <el-table-column label="业务code" :align="'center'" prop="businessCode"/>
      <el-table-column label="操作人员" :align="'center'" prop="operName"/>
      <el-table-column label="操作状态" :align="'center'" prop="status">
        <template #default="scope">
          <el-tag
            :style="scope.row.status == '0' ? 'background-color: #02a30f; color: #fff; font-weight: bold;' : 'background-color: #c70505; color: #fff; font-weight: bold;'">
            {{ getStatusLabel(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" :align="'center'" prop="operTime" width="180">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.operTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" :align="'center'" class-name="small-padding fixed-width" width="220">
        <template #default="scope">
          <el-tooltip  class="item" content="详细信息" effect="dark"  placement="right">
            <el-button circle type="warning" @click="detail(scope.row)">
              <svg-icon icon-class="cheXiao"/>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 详细信息弹出框 -->
    <el-dialog :close-on-click-modal="true" :title="title" v-model="open" width="40%" :fullscreen="false" class="custom-dialog">
      <el-form ref="formRef" :model="form" label-width="120px">
        <el-form-item label="业务code">
          <span class="dialog-content">{{ form.businessCode }}</span>
        </el-form-item>
        <el-form-item label="操作人员" prop="operName">
          <span class="dialog-content">{{ form.operName }}</span>
        </el-form-item>
        <el-form-item label="请求参数" prop="operParam">
          <div class="param-container">
            <pre class="param-content">
              {{ tryParseJSON(form.operParam) }}
            </pre>
          </div>
        </el-form-item>
        <el-form-item label="返回参数" prop="jsonResult">
          <div class="param-container result-container">
            <pre v-if="form.status == 0" class="param-content">
              {{ tryParseJSON(form.jsonResult) }}
            </pre>
            <pre v-if="form.status == 1" class="param-content">
              {{ tryParseJSON(form.errorMsg) }}
            </pre>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="cancel">关 闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
  name: "Log"
}
</script>

<script setup lang="ts">
import {selectPageLog} from "@/api/msip/log.ts";
import {useUserStoreHook} from "@/store/modules/user.ts";

export interface Log extends PageQuery {
  businessCode: string,
  operName: string,
  status: string,
  operTime: string,
  orgId: string,
}

const statusDict = [
  { label: '全部', value: null },
  { label: '异常', value: '1' },
  { label: '正常', value: '0' },
];

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const {proxy} = getCurrentInstance() as any;
const date = proxy.$parseTime(new Date(), "{y}-{m}-{d} ");
const formRef = ref();
const queryParamsRef = ref()
const logList = ref([]);
const open = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = ref({
  form: {
    businessCode: "",
    operName: "",
    status: null,
    operTime: "",
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    businessCode: "",
    operName: "",
    status: null,
    operTime: date,
    orgId: userInfo.orgId,
  }
});

const {queryParams, form} = toRefs(data.value);

/** 查询医保日志记录列表 */
function getList() {
  debugger
  selectPageLog(queryParams.value).then((response: any) => {
    logList.value = response.rows;
    total.value = response.total;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

const getStatusLabel = (statusValue) => {
  const found = statusDict.find(item => item.value == statusValue);
  return found ? found.label : statusValue;
}
// 表单重置
function reset() {
  form.value = {
    id: "",
    businessCode: "",
    requestMethod: "",
    operName: "",
    operId: "",
    orgId: "",
    operParam: null,
    jsonResult: null,
    status: null,
    errorMsg: null,
    operTime: date,
    encJson: ""
  }
  nextTick(() => {
    formRef.value.resetFields()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  // proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
// 详细按钮
function detail(row: any) {
  open.value = true;
  form.value = row;
  title.value = "日志详细信息";
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('msip/log/export', {
    ...queryParams.value
  }, `log_${new Date().getTime()}.xlsx`)
}

// 新增 tryParseJSON 方法
const tryParseJSON = (jsonString: string) => {
  try {
    return JSON.stringify(JSON.parse(jsonString), null, 2);
  } catch (e) {
    return jsonString;
  }
};

getList();
</script>

<style scoped>
.custom-dialog {
  padding: 20px;
}

.dialog-content {
  font-size: 14px;
  color: #606266;
}

.param-container {
  border: 1px solid #ebeef5;
  padding: 15px;
  border-radius: 4px;
  background-color: #e5edeb;
  height: 40vh;
  width: 100%;
  overflow: auto;
}

.result-container {
  height: 40vh;
  width: 100%;
}

.param-content {
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>
